﻿using System;
using System.Collections.Generic;
using Entities;
using System.Data.Common;
using WebLog;
namespace Data
{
    public sealed class UserData
    {
        public Users GetUser(Users i)
        {
            int body = Convert.ToInt32(i.rut.Substring(0, i.rut.Length - 1));
            string digit = i.rut.Substring(i.rut.Length - 1, 1);
            DataBase bd = new DataBase();
            Users uc = null;
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("OBTENER_USUARIO");
                bd.AssignInt("@rut_usuario", body);
                bd.AssignString("@DigitoVerificador_usuario", digit);
                bd.AssignString("@Contrasena_usuario", i.password);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        uc = LoadUser(readerDataSource);
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    uc = null;
                }
            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
                uc = null;
            }

            finally
            {

                bd.Disconnect();
                bd.ReleaseResources();
            }
            return uc;
        }

        public Users GetUserById(int id)
        {
            DataBase bd = new DataBase();
            Users uc = null;
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("OBTENER_USUARIO_POR_ID");
                bd.AssignInt("@id_usuario", id);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        uc = LoadUser(readerDataSource);
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    uc = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                uc = null;
            }

            finally
            {
                bd.Disconnect();
                bd.ReleaseResources();
            }
            return uc;
        }

        public List<Rol> GetListRolesById(int userId)
        {
            List<Rol> listRole = new List<Rol>();
            DataBase bd = new DataBase();
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("OBTENER_ROLES_USUARIO");
                bd.AssignInt("@id_usuario", userId);
                DbDataReader readerDataSource = bd.ExecuteDataReader();

                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        listRole.Add(new Rol() { id = readerDataSource.GetInt32(0).ToString(), description = readerDataSource.GetString(1) });

                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    listRole = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                listRole = null;
            }
            finally
            {
                bd.Disconnect();
                bd.ReleaseResources();
            }
            return listRole;
        }

        public bool InsertUser(Users i)
        {
            bool response = false;
            string body = i.rut.Substring(0, i.rut.Length - 1);
            string digit = i.rut.Substring(i.rut.Length - 1, 1);
            DataBase bd = new DataBase();
            try
            {
                bd.Connect();
                bd.BeginTransaction();
                bd.ProcedureSqlName("INSERTAR_USUARIO");
                bd.AssignString("@rut_usuario", body);
                bd.AssignString("@DigitoVerificador_usuario", digit);
                bd.AssignString("@Nombre_usuario", i.name);
                bd.AssignString("@Segundo_Nombre_Usuario", i.middleName);
                bd.AssignString("@Apellido_usuario", i.lastName);
                bd.AssignString("@Segundo_Apellido_Usuario", i.secondLastName);
                bd.AssignString("@Contrasena_usuario", i.password);
                bd.AssignString("@Emai_Usuario", i.email);
                bd.AssignBool("@Bloqueado_usuario", i.blocked);
                bd.AssignDateTime("@Fecha_Inscripcion_usuario", i.registrationDate);
                bd.AssignString("@Telefono_Usuario", i.telephone);
                bd.AssignString("@Celular_Usuario", i.cellPhone);

                int id = bd.ExecuteScalar();
                if (id > 0)
                {
                    int registroAfectado = 0;

                    foreach (Rol r in i.usersRoles)
                    {
                        bd.ProcedureSqlName("INSERTAR_ROL_USUARIO");
                        bd.AssignInt("@id_role", Convert.ToInt32(r.id));
                        bd.AssignInt("@id_usuario", id);
                        registroAfectado += bd.ExecuteNonQuery();
                    }


                    if (registroAfectado == i.usersRoles.Count)
                    {
                        bd.CommitTransaction();
                        response = true;
                    }
                    else
                    {
                        response = false;
                        bd.RollbackTransaction();
                    }
                }
                else
                {
                    response = false;
                    bd.RollbackTransaction();
                }

            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                response = false;
                bd.RollbackTransaction();
            }

            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }

            return response;
        }

        public List<Users> SearchUsersByRut(Users i)
        {
            int body = Convert.ToInt32(i.rut.Substring(0, i.rut.Length - 1));
            string digit = i.rut.Substring(i.rut.Length - 1, 1);
            DataBase bd = new DataBase();
            DbDataReader readerDataSource = null;
            List<Users> list = new List<Users>();
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_RUT_USUARIO");
                bd.AssignInt("@rut_usuario", body);
                bd.AssignString("@DigitoVerificador_usuario", digit);

                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }

            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        //public List<Users> BuscarPorRutUsuarioDatos(string rut)
        //{
        //    int body = Convert.ToInt32(rut.Substring(0, rut.Length - 1));
        //    string digit = rut.Substring(rut.Length - 1, 1);
        //    DataBase bd = new DataBase();
        //    List<Users> group = new List<Users>();
        //    DbDataReader readerDataSource = null;
        //    try
        //    {
        //        bd.Connect();
        //        bd.ProcedureSqlName("BUSCAR_POR_RUT_USUARIO");
        //        bd.AssignInt("@rut_usuario", body);
        //        bd.AssignString("@DigitoVerificador_usuario", digit);
        //        readerDataSource = bd.ExecuteDataReader();
        //        if (readerDataSource.HasRows)
        //        {
        //            while (readerDataSource.Read())
        //            {
        //                group.Add(LoadUser(readerDataSource));
        //            }
        //            readerDataSource.Close();
        //            readerDataSource.Dispose();
        //        }
        //        else
        //        {
        //            group = null;
        //        }


        //    }

        //    catch (Exception e)
        //    {
        //        group = null;
        //    }
        //    finally
        //    {
        //        bd.ReleaseResources();
        //        bd.Disconnect();
        //    }
        //    return group;
        //}

        public List<Users> SearchUsersByName(string name)
        {

            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_NOMBRE_USUARIO");
                bd.AssignString("@Nombre_usuario", name);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }

            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByLastName(string lastName)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_APELLIDO_USUARIO");
                bd.AssignString("@Apellido_usuario", lastName);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByEmail(string email)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_EMAIL_USUARIO");
                bd.AssignString("@Emai_Usuario", email);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByTelephone(string telephone)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_TELEFONO_USUARIO");
                bd.AssignString("@Telefono_Usuario", telephone);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByCellPhone(string cellPhone)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_CELULAR_USUARIO");
                bd.AssignString("@Celular_Usuario", cellPhone);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByDate(DateTime date)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_FECHA_INSCRIPCION_USUARIO");
                bd.AssignDateTime("@Fecha_Inscripcion_usuario", date);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByBlocked(bool blocked)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_BLOQUEADO_USUARIO");
                bd.AssignBool("@Bloqueado_usuario", blocked);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> SearchUsersByRol(string rol)
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("BUSCAR_POR_NOMBRE_ROL");
                bd.AssignString("@ROL", rol);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<Users> GetListUsers()
        {
            DataBase bd = new DataBase();
            List<Users> list = new List<Users>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("OBTENER_USUARIOS");
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(LoadUser(readerDataSource));
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<string> AutocompleteByName(string name)
        {
            DataBase bd = new DataBase();
            List<string> list = new List<string>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("AUTOCOMPLETAR_NOMBRE_USUARIO");
                bd.AssignString("@Nombre_usuario", name);
                readerDataSource = bd.ExecuteDataReader();

                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(readerDataSource.GetString(0));
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {

                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<string> AutocompleteByLastName(string lastName)
        {
            DataBase bd = new DataBase();
            List<string> list = new List<string>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("AUTOCOMPLETAR_APELLIDO_USUARIO");
                bd.AssignString("@Apellido_usuario", lastName);
                readerDataSource = bd.ExecuteDataReader();

                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(readerDataSource.GetString(0));
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {

                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public List<string> AutocompleteByRut(string rut)
        {
            DataBase bd = new DataBase();
            List<string> list = new List<string>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("AUTOCOMPLETAR_RUT_USUARIO");
                bd.AssignString("@rut_usuario", rut);
                readerDataSource = bd.ExecuteDataReader();

                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(readerDataSource.GetString(0).ToString() + readerDataSource.GetString(1));
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();

            }
            return list;
        }

        public List<string> AutocompleteByEmail(string email)
        {
            DataBase bd = new DataBase();
            List<string> list = new List<string>();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("AUTOCOMPLETAR_MAIL_USUARIO");
                bd.AssignString("@Emai_Usuario", email);
                readerDataSource = bd.ExecuteDataReader();

                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        list.Add(readerDataSource.GetString(0));

                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    list = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                list = null;
            }
            finally
            {

                bd.ReleaseResources();
                bd.Disconnect();
            }
            return list;
        }

        public bool DeleteUser(int id)
        {
            DataBase bd = new DataBase();
            bool response = false;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("ELIMINAR_USUARIO");
                bd.AssignInt("@id_usuario", id);
                int query = bd.ExecuteNonQuery();
                if (query > 0)
                {
                    response = true;
                }
                else
                {
                    response = false;
                }
            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                response = false;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return response;
        }

        public bool ChangePassword(Users i)
        {
            DataBase bd = new DataBase();
            bool response = false;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("CAMBIAR_PASSWORD_USUARIO");
                bd.AssignString("@Contrasena_usuario", i.password);
                bd.AssignInt("@Id_usuario", Convert.ToInt32(i.id));

                int registroActualizado = bd.ExecuteNonQuery();
                if (registroActualizado > 0)
                {

                    response = true;
                }
                else
                {
                    response = false;
                }
            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                response = false;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return response;

        }

        public bool UpdateUserById(Users i)
        {
            string body = i.rut.Substring(0, i.rut.Length - 1);
            string digit = i.rut.Substring(i.rut.Length - 1, 1);
            bool response = false;
            DataBase bd = new DataBase();
            try
            {
                bd.Connect();
                bd.BeginTransaction();
                bd.ProcedureSqlName("ACTUALIZAR_USUARIO");
                bd.AssignInt("@id_usuario", Convert.ToInt32(i.id));
                bd.AssignString("@rut_usuario", body);
                bd.AssignString("@DigitoVerificador_usuario", digit);
                bd.AssignString("@Nombre_usuario", i.name);
                bd.AssignString("@Segundo_Nombre_Usuario", i.middleName);
                bd.AssignString("@Apellido_usuario", i.lastName);
                bd.AssignString("@Segundo_Apellido_Usuario", i.secondLastName);
                bd.AssignString("@Emai_Usuario", i.email);
                bd.AssignBool("@Bloqueado_usuario", i.blocked);
                bd.AssignDateTime("@Fecha_Inscripcion_usuario", i.registrationDate);
                bd.AssignString("@Telefono_Usuario", i.telephone);
                bd.AssignString("@Celular_Usuario", i.cellPhone);
                int registroActualizado = bd.ExecuteNonQuery();

                if (registroActualizado > 0)
                {
                    int registroAfectado = 0;
                    bd.ProcedureSqlName("ELIMINAR_ROL_USUARIO");
                    bd.AssignInt("@id_usuario", Convert.ToInt32(i.id));
                    int elimina = bd.ExecuteNonQuery();

                    if (elimina > 0)
                    {
                        foreach (Rol r in i.usersRoles)
                        {
                            bd.ProcedureSqlName("INSERTAR_ROL_USUARIO");
                            bd.AssignInt("@id_role", Convert.ToInt32(r.id));
                            bd.AssignInt("@id_usuario", Convert.ToInt32(i.id));
                            registroAfectado += bd.ExecuteNonQuery();
                        }


                        if (registroAfectado == i.usersRoles.Count)
                        {
                            bd.CommitTransaction();
                            response = true;
                        }
                        else
                        {
                            response = false;
                            bd.RollbackTransaction();
                        }
                    }
                    else
                    {
                        response = false;
                        bd.RollbackTransaction();
                    }
                }
                else
                {
                    response = false;
                    bd.RollbackTransaction();
                }
            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                bd.RollbackTransaction();
                response = false;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }
            return response;

        }

        public List<Rol> GetListRoles()
        {
            List<Rol> roles = new List<Rol>();
            DataBase bd = new DataBase();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.ProcedureSqlName("OBTENER_ROLES");
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        roles.Add(new Rol()
                        {
                            id = readerDataSource.GetInt32(0).ToString(),
                            description = readerDataSource.GetString(1)
                        });
                    }
                    readerDataSource.Close();
                    readerDataSource.Dispose();
                }
                else
                {
                    roles = null;
                }
            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
                roles = null;
            }
            finally
            {

                bd.Disconnect();
                bd.ReleaseResources();
            }
            return roles;
        }

        private Users LoadUser(DbDataReader readerDataSource)
        {
            Users uc = new Users();
            uc.id = readerDataSource.GetInt32(0).ToString();
            uc.rut = readerDataSource.GetString(1) + readerDataSource.GetString(2);
            uc.name = readerDataSource.GetString(3);
            uc.middleName = readerDataSource.GetString(4);
            uc.lastName = readerDataSource.GetString(5);
            uc.secondLastName = readerDataSource.GetString(6);
            uc.password = readerDataSource.GetString(7);
            uc.email = readerDataSource.GetString(8);
            uc.blocked = readerDataSource.GetBoolean(9);
            uc.registrationDate = readerDataSource.GetDateTime(10);
            uc.telephone = readerDataSource.GetString(11);
            uc.cellPhone = readerDataSource.GetString(12);
            uc.dateView = uc.registrationDate.ToShortDateString();
            uc.usersRoles = GetListRolesById(Convert.ToInt32(uc.id));
            if (uc.blocked)
            {
                uc.blockedView = "Si";
            }
            else
            {
                uc.blockedView = "No";
            }

            foreach (Rol r in uc.usersRoles)
            {
                uc.rolesView += r.description + "</br>";
            }

            return uc;
        }
    }
}
